package com.podium.web.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.podium.web.system.entity.SysRoleMenu;
import com.podium.web.system.mapper.SysRoleMenuMapper;
import com.podium.web.system.service.SysRoleMenuService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 邵文泽
 * @description 公共表单
 * @date Tue Apr 29 17:10:47 CST 2025
 */
@Slf4j
@Service
public class SysRoleMenuServiceImpl extends ServiceImpl<SysRoleMenuMapper, SysRoleMenu> implements SysRoleMenuService {

    @Override
    public IPage<SysRoleMenu> page(Page<SysRoleMenu> page, SysRoleMenu sysRoleMenu) {
        QueryWrapper<SysRoleMenu> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .eq(sysRoleMenu.getRoleId() != null, SysRoleMenu::getRoleId, sysRoleMenu.getRoleId())
                .eq(sysRoleMenu.getMenuId() != null, SysRoleMenu::getMenuId, sysRoleMenu.getMenuId())
        ;
        return super.page(page, queryWrapper);
    }

    @Override
    public List<SysRoleMenu> list(SysRoleMenu sysRoleMenu) {
        QueryWrapper<SysRoleMenu> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .eq(sysRoleMenu.getRoleId() != null, SysRoleMenu::getRoleId, sysRoleMenu.getRoleId())
                .eq(sysRoleMenu.getMenuId() != null, SysRoleMenu::getMenuId, sysRoleMenu.getMenuId())
        ;
        return super.list(queryWrapper);
    }

    @Override
    public SysRoleMenu getById(Long id) {
        return super.getById(id);
    }

    @Override
    public boolean save(SysRoleMenu sysRoleMenu) {
        return super.save(sysRoleMenu);
    }

    @Override
    public boolean updateById(SysRoleMenu sysRoleMenu) {
        return super.updateById(sysRoleMenu);
    }

    @Override
    public void removeById(Long id) {
        super.removeById(id);
    }

    @Override
    public void removeByRoleId(Long roleId) {
        super.remove(new QueryWrapper<SysRoleMenu>().lambda().eq(SysRoleMenu::getRoleId, roleId));
    }

    @Override
    public boolean saveBatch(SysRoleMenu sysRoleMenu) {
        List<SysRoleMenu> list = new ArrayList<>();
        for (Long menuId : sysRoleMenu.getMenuIds()) {
            SysRoleMenu roleMenu = new SysRoleMenu();
            roleMenu.setRoleId(sysRoleMenu.getRoleId());
            roleMenu.setMenuId(menuId);
            list.add(roleMenu);
        }
        return super.saveBatch(list);
    }

    @Override
    public List<SysRoleMenu> listByRoleId(List<Long> roleIds) {
        QueryWrapper<SysRoleMenu> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().in(SysRoleMenu::getRoleId, roleIds);
        return super.list(queryWrapper);
    }

}